<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avator_box">
                <img src="../assets/123.jpg" alt="">
            </div>
            <el-form ref="loginRef" class="login_form" :model="loginForm" :rules="loginFormRules">
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" prefix-icon="iconfont icon-3702mima" v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item class="btns_form">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="danger" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>


<script>
export default {
    data(){
        return {
            loginForm:{
                username:"admin",
                password:"123456"
            },
            // 验证表单
            loginFormRules: {
                username:[
                    { required:true,message:'用户名不能为空',trigger:'blur' },
                    { min:3, max:10, message:'长度在3~10之间',trigger:'blur' }
                    ],
                password:[
                    { required:true,message:'密码不能为空',trigger:'blur' },
                    { min:6,max:15,message:'长度在3~5之间',trigger:'blur' }
                ]
            }
        }
    },
    // 重置表单
    methods:{
        reset(){
            this.$refs.loginRef.resetFields()
        },
        login(){
            this.$refs.loginRef.validate(async valid=>{
                if(!valid) return;
                let {data:res} = await this.$http.post('login',this.loginForm)
                if(res.meta.status != 200) return this.$message.error("登录失败，请重新登录")
                sessionStorage.setItem("token",res.data.token)
                this.$router.push("/home")
                this.$message.success("登录成功")
            })
        }
    }
}
</script>

<style lang="less" scoped>
.login_container {
    background-color: #2b4b6b;
    height: 100%;
}
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    .avator_box {
        width: 130px;
        height: 130px;
        background-color: #fff;
        border: 1px solid #eee;
        border-radius: 50%;
        box-shadow: 0 0 10px #ccc;
        text-align: center;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        img {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            margin-top: 5px;
        }
    }
}
.avator_box:hover {
    transition: all 1s;
    width: 160px;
    height: 160px;
    background-color: #fff;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #333;
    text-align: center;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    img {
        transition: all 1s;
        width: 150px;
        height: 150px;
        border-radius: 50%;
        margin-top: 5px;
        transform: rotate(360deg)
    }
}

.login_form {
    margin-top: 100px;
    padding: 0 20px;
    .btns_form {
        display: flex;
        justify-content: flex-end;
    }
}
</style>



